<?php
namespace app\admin\controller;

use think\Db;
use think\Loader;
use think\Config;
use think\Log;
use think\Response;

class AuthRule extends Admin
{
    public function index()
    {
        //查询条件
        $where = [];

        //排序字段
        $order_by = ['id' => 'asc'];

        //查询数据
        $list = Db::name('AuthRule')->where($where)->order($order_by)->select();
        $count = count($list);

        $this->view->assign('count', $count);
        $this->view->assign('list', $this->get_tree2($list));

        return $this->view->fetch();
    }

    /*
     * 非递归无限级分类(排好序的二维数组)
     */
    private function get_tree2(&$data, $pid = 0, $level = 0)
    {
        if (!isset($data['old'])) {
            $data = array('new' => array(), 'old' => $data);
        }
        foreach ($data['old'] as $key => $value) {
            if ($value['pid'] == $pid) {
                $value['tree_level'] = $level;
                $data['new'][] = $value;
                unset($data['old'][$key]);
                $this->get_tree2($data, $value['id'], $level + 1);
            }
        }
        return $data['new'];
    }

    public function add()
    {
        if ($this->request->isAjax()) {

            $data = $this->request->param();

            // 验证
            $validate = Loader::validate('AuthRule');
            if (!$validate->scene('add')->check($data)) {
                return ajax_return_error($validate->getError());
            }

            $insert_id = Db::name('AuthRule')->insert($data);
            if ($insert_id > 0) {
                return ajax_return('添加成功');
            } else {
                return ajax_return_error('添加失败');
            }

        } else {
            $res = Db::name('AuthRule')->order(['id'=>'asc'])->select();
            $ruleList = get_menu($res);
            $this->view->assign('ruleList',$ruleList);
            return $this->view->fetch();
        }
    }

    public function edit()
    {
        if ($this->request->isAjax()) {

            $data = $this->request->param();
            if (!$data['id']) {
                return ajax_return_error('缺少参数ID');
            } else {
                $id = $data['id'];
                unset($data['id']);
            }

            // 验证
            $validate = Loader::validate('AuthRule');
            if (!$validate->scene('edit')->check($data)) {
                return ajax_return_error($validate->getError());
            }

            $update_id = Db::name('AuthRule')->where('id', $id)->update($data);
            if ($update_id > 0) {
                return ajax_return('编辑成功');
            } else {
                return ajax_return_error('编辑失败');
            }
        } else {
            $id = $this->request->param('id');
            $li = Db::name('AuthRule')->where('id', $id)->find();

            $res = Db::name('AuthRule')->order(['id'=>'asc'])->select();
            $ruleList = get_menu($res);

            $this->view->assign('id', $id);
            $this->view->assign('li', $li);
            $this->view->assign('ruleList',$ruleList);

            return $this->view->fetch();
        }
    }

    public function delete()
    {
        $ids = $this->request->param('id');
        $where['id'] = ['in', $ids];
        $re = Db::name('AuthRule')->where($where)->delete();
        if ($re > 0) {
            return ajax_return('删除成功');
        } else {
            return ajax_return_error('删除失败');
        }
    }

}